<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Transaction;

class TransactionController extends Controller
{
    //
    public function getTransaction(Request $request){
        $transaction = Transaction::where('type',$request->get('type')) ->orderBy('id')-> get();
        return response([
            'data' => $transaction,
            'code' => 200
        ]);
    }

        public function addTransaction(Request $request){
//        $new = News::where('type',$request->get('type'));
        $data = $request->data;
//        dd($data['add']);
        foreach($data['add']??[] as $key => $item){
            Transaction::create([
                'loads' => $item['loads'],
                'old' => $item['old'],
                'status' => $request->get('status'),
                'type' => $request->get('type'),
            ]);
        }
        Transaction::destroy(array_pluck($data['remove']??[],'id'));
        foreach(
            $data['update']??[] as $key => $item){
            Transaction::find($item['id'])->update([
                'loads' => $item['loads'],
                'old' => $item['old'],
                'status' => $request->get('status'),
                'type' => $request->get('type'),
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
